<?php
/**
 * 修改sql为链式写法，防止sql注入
* @authors xiadx (xiadx@zpdchina.com)
* @date    2017-02-24 16:14:34
* @version $Id$
*/
class YizhuHistoryModel extends BaseModel
{
	/**
	 * 返回给终端的可执行sql语句的字段名称数组
	 * @var [type]
	 */
	public $_field_arr = ['history_id','zhuyuan_id','zhixing_state','zhixing_type','zhixing_hushi_id','zhixing_hushi_name','other_info','zhixing_zuhao','zhixing_time','type','beizhu','real_time','hedui_cishu','op_type','dangqian_cishu','yizhu_time','yizhu_shuxing','modify_time','show_zhixing_state'];

	/**
	 * 获取历史医嘱数据
	 * @param  [type] $user_department [description]
	 * @param  [type] $zhuyuan_id      [description]
	 * @param  [type] $json_data       [description]
	 * @param  string $modify_time     [description]
	 * @return [type]                  [description]
	 */
	public function getYizhuHistoryDataCommon($user_department, $zhuyuan_id, $json_data , $modify_time = '')
	{
		$field = 'a.id as history_id,a.zhuyuan_id,a.zhixing_state,
        a.zhixing_type,a.zhixing_hushi_id,a.zhixing_hushi_name,
        a.other_info,a.zhixing_zuhao,a.zhixing_time,
        a.type,a.beizhu,a.business_time as real_time,a.hedui_cishu,
        a.op_type,a.dangqian_cishu,a.yizhu_time,
        a.yizhu_shuxing,a.create_time as modify_time,a.show_zhixing_state';
		$query = DB::select($field)
		->from('zhuyuan_basic_info as t')
		->join('zhuyuan_yizhu_zhixing_history as a')
		->on('t.zhuyuan_id', '=', 'a.zhuyuan_id')
		->where('real_time', '>=', DB::expr('DATE_SUB(curdate(), INTERVAL 1 DAY)'));
		if ($zhuyuan_id) {
			$query->where('t.zhuyuan_id', '=', $zhuyuan_id);
		}

		if ($user_department) {
			$query->and_where('t.zhuyuan_bingqu', '=', $user_department);
		}

		if ($modify_time) {
			$query->and_where('a.create_time', '>=', $modify_time);
		}

		if (!empty($json_data)) {
			$query->and_where('a.id', 'IN', $json_data);
		}

		$query->and_where('t.yiyuan_id', '=', $this->yiyuan_id);

		/*处理患者保留时间*/
		$yiyuan_id = $this->yiyuan_id;
		$huanzhe_chuyuan_baoliu_shijian = Yaconf::get("config$yiyuan_id.huanzhe_chuyuan_baoliu_shijian");

		if ($huanzhe_chuyuan_baoliu_shijian) {
			$intval = Utility::getIntervalSqlArray('t.chuyuan_riqi_time',$huanzhe_chuyuan_baoliu_shijian);
			$query->and_where_open()
			->where('t.zhuangtai', '=', '住院中')
			->or_where_open()
			->where('t.zhuangtai', '=', '已出院')
			->and_where('t.chuyuan_riqi_time', '>=', DB::expr($intval))
			->or_where_close()
			->and_where_close();
		}
		return $query->execute()->as_array();
	}

	/**
	 * 处理上传数据
	 * @param array $data
	 */
	public function yizhuHistory($data = [], $online_submit_data = 'off', $yiyuan_id){

		$re = [];
		if(count($data) > 1){
			//数据排序
			$data = self::uploadSortArr($data, $yiyuan_id);
		}
		//获取所有组号
		$zuhao_arr = array_column($data, 'zhixing_zuhao');

		//获取所有操作时间
		$yizhu_time_arr = array_column($data, 'yizhu_time');

		//获取最后的状态
		$last_data = self::getHistoryLastData($data, $zuhao_arr, $yizhu_time_arr, $yiyuan_id);

		$app_config = self::getAppConfig($yiyuan_id);
		$zuhao_arrs = [];//推送医嘱信息
		$insert_data = [];//推送操作信息

		foreach($data as $k=>$one){

			if(self::existLockZuhao($one['zhixing_zuhao'], $one['type'] == '长期' ? $one['yizhu_time'] : '', $yiyuan_id)){//存在冲突数据
                if(self::addHistoryData($one, $online_submit_data, 5000, $yiyuan_id)){
					$re[] = ['history_id'=>$one['history_id'], 'state'=>'1002'];
				}else{
					$re[] = ['history_id'=>$one['history_id'], 'state'=>'1005'];
				}
			}else{//没锁处理数据
				$re_code = @self::opFlow($one, $last_data, $app_config, $yiyuan_id);

				if($re_code['state'] < 300){
					//$st = self::addYizhuHistory($one, $yiyuan_id);
					if(self::addYizhuHistory($one, $yiyuan_id)){//添加成功
						//更新最后数据状态
						if($one['type'] == '长期'){
							$last_data[$one['zhixing_zuhao'].$one['yizhu_time']] = $one;
						}else{
							$last_data[$one['zhixing_zuhao']] = $one;
						}
						$re[] = ['history_id'=>$one['history_id'], 'state'=>'1001'];
						//推送医嘱信息
						$zuhao_arrs[$one['zhixing_zuhao']]['modify_time'] = $one['real_time'];
						$zuhao_arrs[$one['zhixing_zuhao']]['zhixing_state'] = $one['zhixing_state'] != '执行完毕' ? $one['zhixing_state'] : $one['chushi_state'];
						$zuhao_arrs[$one['zhixing_zuhao']]['wancheng_cishu'] = $one['zhixing_state'] != '执行完毕' ? $one['dangqian_cishu']-1 : $one['dangqian_cishu'];
						$zuhao_arrs[$one['zhixing_zuhao']]['yizhu_time'] = $one['yizhu_time'];
						//add show_zhixing_state by zhanghong
                        $zuhao_arrs[$one['zhixing_zuhao']]['show_zhixing_state'] = $one['show_zhixing_state'];
                        //end

						//推送操作数据
						$insert_data[$k] = $one;
						$insert_data[$k]['id'] = $one['history_id'];

					}else{
						$re[] = ['history_id'=>$one['history_id'], 'state'=>'1004'];
					}
					self::addHistoryData($one, $online_submit_data, $re_code['state'], $yiyuan_id);
				}else{
                    //数据有问题，需要枷锁
					if($online_submit_data == 'off'){
						self::addLockZuhao($one['zhixing_zuhao'], $one['type'] == '长期' ? $one['yizhu_time'] : '', $yiyuan_id);//枷锁
						//推送枷锁信息
						$zuhaos[$one['zhixing_zuhao']]['yizhu_time'] = $one['yizhu_time'];
						$zuhaos[$one['zhixing_zuhao']]['yichang_lock'] = 1;
						self::pushMsgInsertQueese(['lock_arr'=>$zuhaos,'set_data'=>[]]);
					}
					if(self::addHistoryData($one, $online_submit_data, $re_code['state'], $yiyuan_id)){
						$re[] = ['history_id'=>$one['history_id'], 'state'=>'1003'];
					}else{
						$re[] = ['history_id'=>$one['history_id'], 'state'=>'1005'];
					}
				}
			}
		}
		self::pushMsgInsertQueese(['zuhao_arr'=>$zuhao_arrs,'history_data'=>$insert_data,'set_data'=>[]]);
		return $re;
	}


	/**
	 *
	 * @param array $data
	 */
	public function opFlow($one = [], $last_data = [], $app_config = [], $yiyuan_id){
        $re = [];
		if(($one['type'] == '长期' && ($one['real_time'] >= $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['real_time']) || !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]))|| ($one['type'] == '临时' && ( $one['real_time'] >= $last_data[$one['zhixing_zuhao']]['real_time'] || !isset($last_data[$one['zhixing_zuhao']])))){
			switch ($one['zhixing_state']){
				//待配液
				case '待配液':
					if($one['op_type'] == '配前核对'){
						if($app_config['pq_cishu']){
							if($last_data){
								$sql = "SELECT COUNT(*)+1 AS tp_count FROM `zhuyuan_yizhu_zhixing_history` WHERE ( `zhuyuan_id` = '".$one['zhuyuan_id']."' ) AND ( `zhixing_zuhao` = '".$one['zhixing_zuhao']."' ) AND ( `zhixing_state` = '".$one['zhixing_state']."' ) AND ( `op_type` = '".$one['op_type']."' ) ";
								if($one['type'] == '长期'){
									$sql .= " and yizhu_time='".$one['yizhu_time']."'";
								}
								$sql .= " limit 1";
								$zhixing_cishu = DB::query(Database::SELECT, $sql)->execute()->as_array()['0']['tp_count'];
							}else{
								$zhixing_cishu = 1;
							}

							if($zhixing_cishu > $app_config['pq_cishu']){
								$re[] = ['history_id'=>$one['history_id'], 'state'=>50102];//配前次数超标
							}else{
								switch (true){
									case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']]))) && $one['hedui_cishu'] == $zhixing_cishu  ://配前的上一步为空
										$re[] = ['history_id'=>$one['history_id'], 'state'=>201];//
										break;
									case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))  && $one['hedui_cishu'] == $zhixing_cishu ://配前的上一步为上一周期执行完毕
										$re[] = ['history_id'=>$one['history_id'], 'state'=>202];//
										break;
									case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '待配液' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '待配液' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  && $one['hedui_cishu'] == $zhixing_cishu ://配前的上一步为本周期待配液
										$re[] = ['history_id'=>$one['history_id'], 'state'=>203];//
										break;
									default:
										$re[] = ['history_id'=>$one['history_id'], 'state'=>50103];//配前次数超标
										break;
								}
							}
						}else{
							$re[] = ['history_id'=>$one['history_id'], 'state'=>50101];//配前没有打开
						}

					}elseif ($one['op_type'] == '撤销'){
						switch (true){
							case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '已配液' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '已配液' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu']))) ://配前的上一步本周期的配液
								$re[] = ['history_id'=>$one['history_id'], 'state'=>204];//
								break;
							default:
								$re[] = ['history_id'=>$one['history_id'], 'state'=>50104];//配前次数超标
								break;
						}
					}else{
						$re[] = ['history_id'=>$one['history_id'], 'state'=>50100];
					}
					break;
                case '核对1':
                    switch (true){
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))  ://核对1前一步为空
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>220];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))  ://配前的上一步为上一周期执行完毕
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>221];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步为配前
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>222];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步撤销
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>223];//
                            break;
                        default:
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>50112];
                            break;
                    }
                    break;
                case '核对2':
                    switch (true){
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))  ://核对1前一步为空
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>224];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))  ://配前的上一步为上一周期执行完毕
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>225];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步为配前
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>226];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu']))) ://配前的上一步撤销
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>227];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '核对1' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '核对1' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步核对1
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>227];//
                            break;
                        default:
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>50113];
                            break;
                    }
                    break;
                case '核对3':
                    switch (true){
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))  ://核对1前一步为空
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>228];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))  ://配前的上一步为上一周期执行完毕
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>229];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu']))) ://配前的上一步为配前
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>230];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步撤销
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>231];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '核对1' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '核对1' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步核对1
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>232];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '核对2' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '核对2' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步核对2
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>233];//
                            break;
                        default:
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>50114];
                            break;
                    }
                    break;
                case '已配液':
					switch (true){
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))  ://配前的上一步为空
							$re[] = ['history_id'=>$one['history_id'], 'state'=>205];//
							break;
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))://配前的上一步为上一周期执行完毕
							$re[] = ['history_id'=>$one['history_id'], 'state'=>206];//
							break;
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '配前核对' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步为配前
							$re[] = ['history_id'=>$one['history_id'], 'state'=>207];//
							break;
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '撤销' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步撤销
							$re[] = ['history_id'=>$one['history_id'], 'state'=>208];//
							break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '核对1' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '核对1' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步核对1
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>234];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '核对2' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '核对2' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步核对2
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>235];//
                            break;
                        case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '核对3' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '核对3' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu']))) ://配前的上一步核对3
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>236];//
                            break;
						default:
							$re[] = ['history_id'=>$one['history_id'], 'state'=>50105];//配前次数超标
							break;
					}
					break;
				case '已校对':
					switch (true){
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['op_type'] == '配液' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['op_type'] == '配液' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ://配前的上一步配液
							$re[] = ['history_id'=>$one['history_id'], 'state'=>209];//
							break;
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))  ://的上一步为空
							$re[] = ['history_id'=>$one['history_id'], 'state'=>210];//
							break;
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))  ://上一步为上一周期执行完毕
							$re[] = ['history_id'=>$one['history_id'], 'state'=>211];//
							break;
						case in_array($one['yongfa_type'], $app_config['pq_type']) && $one['chushi_state'] == '待配液' && (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '待配液' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '待配液' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))   ://配前的上一步为空
							$re[] = ['history_id'=>$one['history_id'], 'state'=>212];//
							break;
						default:
							$re[] = ['history_id'=>$one['history_id'], 'state'=>50106];//配前次数超标
							break;
					}
					break;
				case '开始执行':
					if($one['op_type'] != '开始执行'){
						if((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '开始执行' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '开始执行' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
							$re[] = ['history_id'=>$one['history_id'], 'state'=>213];//
						}else{
							$re[] = ['history_id'=>$one['history_id'], 'state'=>50107];//配前次数超标
						}
					}else{
						
						if($one['chushi_state'] == '待配液' && in_array($one['yongfa_type'], $app_config['pq_type'])){
							if((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '已校对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '已校对' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
								$re[] = ['history_id'=>$one['history_id'], 'state'=>214];//
							}elseif((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '暂停执行' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '暂停执行' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
								$re[] = ['history_id'=>$one['history_id'], 'state'=>215];//
							}else{
								$re[] = ['history_id'=>$one['history_id'], 'state'=>50108];//配前次数超标
							}
						}else{
							if((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '暂停执行' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '暂停执行' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
								$re[] = ['history_id'=>$one['history_id'], 'state'=>216];//
							}elseif((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu']+1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕'  && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu']+1 == $one['dangqian_cishu'])))){
								$re[] = ['history_id'=>$one['history_id'], 'state'=>217];//
							}
                            elseif((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '已校对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '已校对'  && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
                                $re[] = ['history_id'=>$one['history_id'], 'state'=>219];//
                            }
							elseif((($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))){
								$re[] = ['history_id'=>$one['history_id'], 'state'=>218];//
							}else{
								$re[] = ['history_id'=>$one['history_id'], 'state'=>50109];//配前次数超标
							}
						}
					}
					break;
				case '暂停执行':
					if((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '开始执行' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '开始执行' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
						$re[] = ['history_id'=>$one['history_id'], 'state'=>219];//
					}else{
						$re[] = ['history_id'=>$one['history_id'], 'state'=>50110];//配前次数超标
					}
					break;

				case '执行完毕':
					if((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '开始执行' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '开始执行' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  ){
						$re[] = ['history_id'=>$one['history_id'], 'state'=>220];//
					}elseif((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '暂停执行' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '暂停执行' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))){
						$re[] = ['history_id'=>$one['history_id'], 'state'=>221];//
					}else{
						$re[] = ['history_id'=>$one['history_id'], 'state'=>50110];//配前次数超标
					}
					break;

				case '拒绝执行':
					switch (true){
						case  (($one['type'] == '长期' && !isset($last_data[$one['zhixing_zuhao'].$one['yizhu_time']])) || ($one['type'] == '临时' && !isset($last_data[$one['zhixing_zuhao']])))://配前的上一步为空
							$re[] = ['history_id'=>$one['history_id'], 'state'=>222];//
							break;
						case  (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '执行完毕' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] +1 == $one['dangqian_cishu'])))://配前的上一步为上一周期执行完毕
							$re[] = ['history_id'=>$one['history_id'], 'state'=>223];//
							break;
                        case ((($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '已校对' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '已校对' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))):
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>224];//
                            break;
                        case  (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '待配液' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '待配液' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu']))) :
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>225];//
                            break;
                        case  (($one['type'] == '长期' && $last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['zhixing_state'] == '已配液' && ($last_data[$one['zhixing_zuhao'].$one['yizhu_time']]['dangqian_cishu'] == $one['dangqian_cishu'])) || ($one['type'] == '临时' && $last_data[$one['zhixing_zuhao']]['zhixing_state'] == '已配液' && ($last_data[$one['zhixing_zuhao']]['dangqian_cishu'] == $one['dangqian_cishu'])))  :
                            $re[] = ['history_id'=>$one['history_id'], 'state'=>226];//
                            break;
                        default:
							$re[] = ['history_id'=>$one['history_id'], 'state'=>50111];//配前次数超标
							break;
					}
					break;
				default:
					$re[] = ['history_id'=>$one['history_id'], 'state'=>50112];//配前次数超标
					break;
			}
		}else{
			$re[] = ['history_id'=>$one['history_id'], 'state'=>50113];
		}
		return $re[0];
	}
	/**
	 * 获取app参数配置
	 * dx 2017-03-07 17:35:10
	 * @return array
	 */
	public function getAppConfig($yiyuan_id){
		$re = ['pq_cishu'=>2, 'show_peiye'=>1, 'pq_type'=>['输液']];
		$menu_config = DB::select('`mokuai_name`,`enable`')
		->from('data_menu_config')
		->where('mokuai_name','=', '配前核对')
		->where('yiyuan_id', '=', $yiyuan_id)
		->execute()
		->as_array();
		/*
		 $sql = "SELECT `mokuai_name`,`enable` FROM `data_menu_config` WHERE mokuai_name='配前核对' and yiyuan_id='".$yiyuan_id."'";
		 $menu_config = DB::query(Database::SELECT, $sql)->execute()->as_array();
		 */
		foreach($menu_config as $k=>$v){
			if($v['mokuai_name'] == '配前核对' && $v['enable']){
				$re['pq_cishu'] = 2;//默认配置
			}
		}
		$params_data = [];
		if($re['pq_cishu']){
			$params_data[] = 'YIZHU_PEIQIAN_HEDUI';//医嘱配前核对次数
		}
		$params_data[] = 'PEIYE_HEDUI_SHOW';//配液核对中已配液是否显示
		$params_data[] = 'YIZHU_PEIYE_HEDUI_TYPE';//需要配液核对的医嘱type
		$params_config = DB::select('`param_key`,`param_value`')
		->from('data_basic_params_config')
		->where('param_status','=', 1)
		->and_where('yiyuan_id', '=', $yiyuan_id)
		->and_where('param_key', 'in',  $params_data)
		->execute()
		->as_array();
		/*
		 $sql = "SELECT `param_key`,`param_value` FROM `data_basic_params_config` WHERE ( `param_status` = 1 ) AND ( `param_key` IN ('".implode("','", $params_data)."') ) and yiyuan_id='".$yiyuan_id."'";
		 $params_config = DB::query(Database::SELECT, $sql)->execute()->as_array();
		 */
		foreach($params_config as $v){
			if($v['param_key'] == 'YIZHU_PEIQIAN_HEDUI'){
				$re['pq_cishu'] = $v['param_value'];
			}else if($v['param_key'] == 'PEIYE_HEDUI_SHOW'){
				$re['show_peiye'] = $v['param_value'];
			}else if($v['param_key'] == 'YIZHU_PEIYE_HEDUI_TYPE'){
				$re['pq_type'] = explode("|", $v['param_value']);
			}
		}
		return $re;
	}
	/**
	 * dx 2017-03-07 11:50:57
	 * @param array $upload_data
	 * @return array
	 */
	public  function getHistoryLastData($upload_data = [], $zuhao_arr = [], $yizhu_time_arr = [], $yiyuan_id){

		$re = [];
		//获取所有组号
		$zuhao_arr = !empty($zuhao_arr) ? $zuhao_arr : array_column($upload_data, 'zhixing_zuhao');
		//获取所有操作时间
		$yizhu_time_arr = !empty($yizhu_time_arr) ? $yizhu_time_arr : array_column($upload_data, 'yizhu_time');
		/*
		$sql = DB::select('id,zhixing_zuhao,yizhu_time,type,dangqian_cishu,zhixing_state,real_time,op_type,hedui_cishu')
		->from('zhuyuan_yizhu_zhixing_history')
		->where('zhixing_zuhao', 'IN', $zuhao_arr);
		//if($upload_data['type'] =='长期')
		//{
		$sql->and_where('yizhu_time', 'in', $yizhu_time_arr);
		//}
		$sql->and_where('yiyuan_id', '=', $yiyuan_id)
		->order_by('zhixing_zuhao', 'asc')
		->order_by('dangqian_cishu', 'asc')
		->order_by("case when zhixing_state in('拒绝执行', '待配液' ,'已配液') then 1 when zhixing_state = '已校对' then 2 when zhixing_state in('开始执行', '暂停执行') then 3 else 4 end", 'asc')
		->order_by('real_time', 'asc');
		$zuhao_last_state = $sql->execute()->as_array();
		*/
		 $sql = "select id,zhixing_zuhao,yizhu_time,type,dangqian_cishu,zhixing_state,op_type,hedui_cishu,business_time AS real_time from zhuyuan_yizhu_zhixing_history where  yiyuan_id=".$yiyuan_id." and zhixing_zuhao in('".implode("','", $zuhao_arr)."') and  (case when type ='临时' then  1=1 else yizhu_time in('".implode("','", $yizhu_time_arr)."') end )  ORDER BY `zhixing_zuhao` asc,`dangqian_cishu` asc,case when zhixing_state in('拒绝执行', '待配液' ,'已配液','核对1','核对2','核对3') then 1 when zhixing_state = '已校对' then 2 when zhixing_state in('开始执行', '暂停执行') then 3 else 4 end asc,`business_time` asc";
		 $zuhao_last_state = DB::query(Database::SELECT, $sql)->execute()->as_array();
		 
		foreach($zuhao_last_state as $one){
			if($one['type'] == '长期'){
				$re[$one['zhixing_zuhao'].$one['yizhu_time']] = $one;
			}else{
				if(!isset($re[$one['zhixing_zuhao']]) || $one['real_time'] >= $re[$one['zhixing_zuhao']]['real_time']){
					$re[$one['zhixing_zuhao']] = $one;
				}
			}

		}
		return $re;
	}
	/**
	 * dx 2017-03-07 11:50:57
	 * 对数据进行多重排序
	 * zuhao,dangqian_cishu,zhixing_state,real_time
	 * @param array $upload_data
	 * @return array
	 */
	public function uploadSortArr($upload_data = [], $yiyuan_id){
		$temp1 = array();//操作时间排序
		$temp2 = array();//组号排序
		$temp3 = array();//周期排序
		$temp4 = array();//执行状态排序
		// 准备要排序的数组
		foreach ($upload_data as $one) {
			$temp1[] = $one['real_time'];
			$temp2[] = $one['zhixing_zuhao'];
			//$one['sort_val'] = self::sortUploadData($one['zhixing_state']);
			$temp4[] = self::sortUploadData($one['zhixing_state'], $yiyuan_id);
			$temp3[] = $one['dangqian_cishu'];
		}
		//相同组号的数据，在按照周期,状态，时间排序
		array_multisort($temp2, SORT_STRING, $temp3, SORT_ASC, $temp4, SORT_ASC, $temp1, SORT_ASC, $upload_data);
		return $upload_data;
	}

	/**
	 * dx 2017-03-07 11:50:57
	 * 设置排序字段名对应排序值
	 * @param string $str
	 * @return number
	 */
	public function sortUploadData($str = '', $yiyuan_id){
		$re = 4;
		switch ($str){
			case '待配液':
			case '已配液':
			case '拒绝执行':
				$re = 1;
				break;
			case '已校对':
				$re = 2;
				break;
			case '开始执行':
			case '暂停执行':
				$re = 3;
				break;
			default:
				$re = 4;
				break;
		}
		return $re;
	}


	/**
	 * 插入表
	 * @param array $one
	 * @param number $code
	 * @return boolean
	 */
	public function addYizhuHistory($one = [], $yiyuan_id)
	{
		$exist = '';
		$sql = DB::select('id')
		->from('zhuyuan_yizhu_zhixing_history')
		->where('zhixing_zuhao', '=', $one['zhixing_zuhao'])
		->and_where('yiyuan_id', '=', $yiyuan_id)
		->and_where('zhuyuan_id', '=', $one['zhuyuan_id'])
		->and_where('op_type', '=', $one['op_type'])
		->and_where('dangqian_cishu', '=', $one['dangqian_cishu']);
		if(!in_array($one['op_type'], ['核对','执行完毕'])){
			$sql->and_where('hedui_cishu', '=', $one['hedui_cishu']);
		}
		if($one['type'] == '长期'){
			$sql->and_where('yizhu_time', '=', $one['yizhu_time']);
		}

		/*
		 $sql = "insert into zhuyuan_yizhu_zhixing_history (".$dataIntoSql['val'].",yiyuan_id) select ".$dataIntoSql['con'].",".$yiyuan_id." from DUAL where not exists(select * from zhuyuan_yizhu_zhixing_history where zhuyuan_id='".$one['zhuyuan_id']."' and zhixing_zuhao='".$one['zhixing_zuhao']."' and op_type='".$one['op_type']."' and dangqian_cishu='".$one['dangqian_cishu']."' and  case when op_type in ('核对','执行完毕') then 1=1 else hedui_cishu='".$one['hedui_cishu']."' end and case when type ='临时' then  1=1 else yizhu_time='".$one['yizhu_time']."' end) and yiyuan_id=".$yiyuan_id."";
		 */

		$query = 0;
		$query2 = 0;
		$exist = $sql->execute()->as_array();
		$database = Database::instance();
		$database->begin();
		if(!$exist){
			//处理数据
			$dataIntoSql = self::dataIntoSql($one, $yiyuan_id);
			$query = self::dataAddHistory($dataIntoSql, $yiyuan_id);
			$query2 = self::updateYizhu($one, $yiyuan_id);
		}
		if($query){
			$database->commit();
		}else{
			$database->rollback();
		}

		return $query;
	}

	//增加判断明天配前处理
	public function updateYizhu($one = [], $yiyuan_id){
		if($one['type'] == '临时'){
			$table = 'zhuyuan_yizhu_linshi';
		}else{
			if($one['yizhu_time'] >= date('Y-m-d')){
				$table = 'zhuyuan_yizhu_changqi';
			}else{
				$table = 'zhuyuan_yizhu_unwork';
			}
		}
		
		$sql = DB::update($table);
		$paris = [];
		$wancheng_cishu = 0;
		$paris['modify_time'] = date('Y-m-d H:i:s', time());
		if($one['yizhu_time'] <= date('Y-m-d')){
			if($one['zhixing_state'] =='执行完毕'){
				if($one['dangqian_cishu']){
					$wancheng_cishu = $one['dangqian_cishu'];
				}
				$paris['wancheng_cishu'] = $wancheng_cishu;
				$paris['zhixing_state'] = $one['chushi_state'];
			}else{
				if($one['dangqian_cishu']-1){
					$wancheng_cishu = $one['dangqian_cishu']-1;
				}
				$paris['wancheng_cishu'] = $wancheng_cishu;
				$paris['zhixing_state'] = $one['zhixing_state'];
			}
		}
		$sql->set($paris);
		$sql->where('zuhao', '=', $one['zhixing_zuhao'])
		->and_where('zhuyuan_id', '=', $one['zhuyuan_id'])
		->and_where('yiyuan_id', '=', $yiyuan_id);
		if($one['yizhu_time'] < date('Y-m-d') && $one['type'] != '临时'){
			$sql->and_where('unwork_time', '=', $one['yizhu_time']);
		}
		return $sql->execute();
		/*
		 $sql = "update ";
		 if($one['yizhu_time'] == date('Y-m-d'))	{
		 if($one['type'] == '临时'){
		 $sql .= 'zhuyuan_yizhu_linshi';
		 }else{
		 $sql .= 'zhuyuan_yizhu_changqi';
		 }
		 }else{
		 $sql .= 'zhuyuan_yizhu_unwork';
		 }
		 $sql .= " set modify_time='".date('Y-m-d H:i:s',time())."',";
		 if($one['zhixing_state'] =='执行完毕'){
		 $sql .= "wancheng_cishu='".$one['dangqian_cishu']."',zhixing_state='".$one['chushi_state']."'";
		 }else{
		 $sql .= "wancheng_cishu='".($one['dangqian_cishu'] - 1)."',zhixing_state='".$one['zhixing_state']."'";
		 }
		 $sql .= " where zuhao='".$one['zhixing_zuhao']."' and zhuyuan_id='".$one['zhuyuan_id']."' and yiyuan_id='".$yiyuan_id."'";
		 if($one['yizhu_time'] != date('Y-m-d')){
		 $sql .= " and unwork_time='".$one['yizhu_time']."'";
		 }

		 return DB::query(Database::UPDATE, $sql, false)->execute();
		 */
	}
	/**
	 * 查询是否存在组号锁
	 */
	public function existLockZuhao($zuhao = '', $yizhu_time = '', $yiyuan_id){
		$str = 0;
		$arr = self::selLockZuhao($zuhao, $yizhu_time, $yiyuan_id, 1);
		if(count($arr) > 0){
			$str = 1;
		}

		return $str;
		/*
		 $where = " where 1=1 ";
		 if($zuhao){
		 $where .= " and zuhao='".$zuhao."' ";
		 }
		 if($yizhu_time){
		 $where .= " and yizhu_time='".$yizhu_time."' ";
		 }
		 $sql = "select count(1) as tp_count from zhuyuan_yizhu_lock ".$where." and is_lock=0  and yiyuan_id='".$yiyuan_id."' limit 1";
		 return DB::query(Database::SELECT, $sql)->execute()->offsetGet(0)['tp_count'];
		 */
	}

	/**
	 * 获取组号锁数据
	 */
	public function selLockZuhao($zuhao = '', $yizhu_time = '', $yiyuan_id, $limit = 0){
		$sql = DB::select('zuhao,yizhu_time,yiyuan_id')
		->from('zhuyuan_yizhu_lock')
		->where('is_lock', '!=', 1);
		if($zuhao) $sql->where('zuhao', '=', $zuhao);
		if($yiyuan_id) $sql->and_where('yiyuan_id', '=', $yiyuan_id);
		if($yizhu_time) $sql->and_where('yizhu_time', '=', $yizhu_time);

		if($limit != 1){
			return $sql->limit($limit)->execute()->as_array();
		}else{
			return $sql->execute()->offsetGet(0);
		}
		/*
		 $where = " where 1=1 ";
		 if($zuhao){
		 $where .= " and zuhao='".$zuhao."' ";
		 }
		 if($yizhu_time){
		 $where .= " and yizhu_time='".$yizhu_time."' ";
		 }
		 if($yiyuan_id){
		 $where .= " and yiyuan_id='".$yiyuan_id."' ";
		 }
		 $sql = "select zuhao,yizhu_time,yiyuan_id from zhuyuan_yizhu_lock ".$where." and is_lock=0 limit 1";
		 return DB::query(Database::SELECT, $sql)->execute()->offsetGet(0);
		 */
	}

	/**
	 * 添加锁
	 * @param string $zuhaoyizhu_time
	 * @return number
	 */
	public function addLockZuhao($zuhao = '', $yizhu_time = '', $yiyuan_id){
		$one = [];
		if(!$zuhao){
			return 0;
		}else{
			$one['zuhao'] = $zuhao;
		}
		if($yizhu_time){
			$one['yizhu_time'] = $yizhu_time;
		}
		$one['yiyuan_id'] = $yiyuan_id;
		$one['is_lock'] = 0;
		$one['add_lock_time'] = date('Y-m-d H:i:s',time());
		$one['user_department_id'] = $_REQUEST['user_department_id'];
		return self::add('zhuyuan_yizhu_lock', $one);
		/*
		 $sql = "insert into zhuyuan_yizhu_lock (zuhao,yizhu_time,add_lock_time,is_lock,yiyuan_id) values ('".$zuhao."','".$yizhu_time."','".date('Y-m-d H:i:s',time())."',0,'".$yiyuan_id."')";
		 return DB::query(Database::INSERT, $sql, false)->execute()['1'];
		 */
	}

	/**
	 *
	 * @param string $zuhaoyizhu_time
	 * @return unknown
	 */
	public function delLockZuhao($zuhao = '', $yizhu_time = '', $yiyuan_id, $is_lock = 1, $old_lock = 0){

		$sql = DB::update('zhuyuan_yizhu_lock')
		->set(array('is_lock'  => $is_lock, 'del_lock_time'=> date('Y-m-d H:i:s',time())))
		->where('zuhao','=', $zuhao)
		->and_where('yizhu_time','=',$yizhu_time)
		->and_where('yiyuan_id', '=', $yiyuan_id)
		->and_where('is_lock','=', $old_lock)
		->limit(1);
		return $sql->execute();
		/*
		 $sql = "update zhuyuan_yizhu_lock set is_lock=1,del_lock_time='".date('Y-m-d H:i:s',time())."' where zuhao='".$zuhao."' and yizhu_time='".$yizhu_time."' and is_lock=0 and yiyuan_id='".$yiyuan_id."' limit 1";
		 return DB::query(Database::UPDATE, $sql, false)->execute();
		 */
	}

	/**
	 * 操作数据处理
	 * @param array $one
	 * @return string[]
	 */
	public function dataIntoSql($one = []){
		$re = [];
		foreach($one as $k=>$v){
			if($k =='history_id'){
				$re['id']= $v;
			}elseif($k=='yongfa_type'){
				$re['yizhu_yongfa_type']= $v;
			}else{
				$re[$k]= $v;
			}
		}
		return $re;
		/*
		 //处理数据
		 $val = "create_time,";
		 $con = " '".date('Y-m-d H:i:s',time())."',";
		 foreach($one as $k=>$v){
		 if($k=='history_id'){
		 $val .= "id,";
		 }elseif($k=='yongfa_type'){
		 $val .= "yizhu_yongfa_type,";
		 }else{
		 $val .= $k.",";
		 }
		 $con .= "'".$v."',";
		 }
		 $val = trim($val, ',')."";
		 $con = trim($con, ",");
		 return ['val'=>$val, 'con'=>$con];
		 */
	}

	/**
	 * 数据处理
	 * @param array $one
	 * @param string $online_submit_data
	 */
	public function addHistoryData($one = [], $online_submit_data = 'off', $code, $yiyuan_id){
		$dataIntoSql = self::dataIntoSql($one, $yiyuan_id);
		$res = self::addLogHistory($dataIntoSql, $code, $yiyuan_id);
		if($online_submit_data == 'off'){
			return self::addUnlineHistory($dataIntoSql, $yiyuan_id);
		}else{
            if($code < 300)
            {
                return self::addOnlineHistory($dataIntoSql, $yiyuan_id);
            }else
            {
                return $res;
            }
		}

	}

	/**
	 * 添加离线数据
	 * @param array $one
	 * @param string $state
	 */
	public function addUnlineHistory($one = [], $yiyuan_id){
		$count = DB::select('count(id) as tp_count')
		->from('zhuyuan_yizhu_zhixing_history_unline')
		->where('id', '=', $one['id'])
		->and_where('yiyuan_id', '=', $yiyuan_id)->execute()->as_array()['0']['tp_count'];
		if(!$count){
			$one['yiyuan_id'] = $yiyuan_id;
			//$one['state'] = $state;
			return self::add('zhuyuan_yizhu_zhixing_history_unline', $one);
		}
		return 1;
		/*
		 $sql = "insert into zhuyuan_yizhu_zhixing_history_unline (".$re['val'].",yiyuan_id) values (".$re['con'].",'".$yiyuan_id."')";
		 return DB::query(Database::INSERT, $sql, false)->execute()['1'];
		 */
	}

	/**
	 * 更新离线表
	 * @param array $ids
	 * @param number $val
	 * @return number
	 */
	public function updateUnlineHistory($ids = [], $val = 1, $yiyuan_id){
		if(!$ids) return 0;
		return DB::update('zhuyuan_yizhu_zhixing_history_unline')
		->set(['is_op' => $val, 'op_time' => date('Y-m-d H:i:s',time())])
		->where('id', 'IN', $ids)
		->and_where('yiyuan_id', '=', $yiyuan_id)
		->execute();
		/*
		 $id = '';
		 if(is_array($ids)){
		 $id = implode("','", $ids);
		 }
		 $sql = "update zhuyuan_yizhu_zhixing_history_unline set is_op=".$val.",op_time='".date('Y-m-d H:i:s',time())."' where id in ('".$id."') and yiyuan_id='".$yiyuan_id."'";
		 return DB::query(Database::UPDATE, $sql, false)->execute();
		 */
	}


	/**
	 * 添加在线数据
	 * @param array $one
	 * @param string $state
	 */
	public function addOnlineHistory($one = [], $yiyuan_id){
		$count = DB::select('count(id) as tp_count')
		->from('zhuyuan_yizhu_zhixing_history_online')
		->where('id', '=', $one['id'])
		->and_where('yiyuan_id', '=', $yiyuan_id)->execute()->as_array()['0']['tp_count'];
		if(!$count){
			$one['yiyuan_id'] = $yiyuan_id;
			//$one['state'] = $state;
			return self::add('zhuyuan_yizhu_zhixing_history_online', $one);
		}
		return 1;
		/*
		 $sql = "insert into zhuyuan_yizhu_zhixing_history_online (".$re['val'].",yiyuan_id) values (".$re['con'].",'".$yiyuan_id."')";
		 return DB::query(Database::INSERT, $sql, false)->execute()['1'];
		 */
	}

	/**
	 * 更新在线表
	 * @param array $ids
	 * @param number $val
	 * @return number
	 */
	public function updateOnlineHistory($ids = [], $val = 1, $yiyuan_id){
		if(!$ids) return 0;
		$sql = DB::update('zhuyuan_yizhu_zhixing_history_online')
		->set(['is_op' => $val, 'op_time' => date('Y-m-d H:i:s',time())])
		->where('id', 'IN', $ids)
		->and_where('yiyuan_id', '=', $yiyuan_id);
		return $sql->execute();
		/*
		 $id = '';
		 if(is_array($ids)){
		 $id = implode("','", $ids);
		 }
		 $sql = "update zhuyuan_yizhu_zhixing_history_online set is_op=".$val.",op_time='".date('Y-m-d H:i:s',time())."' where id in ('".$id."') and yiyuan_id='".$yiyuan_id."'";
		 return DB::query(Database::UPDATE, $sql, false)->execute();
		 */
	}
	/**
	 * 操作记录保存
	 * @param array $one
	 * @param string $state
	 * @return number
	 */
	public function addLogHistory($one = [], $state = '50000', $yiyuan_id){
		$dataIntoSql = self::dataIntoSql($one, $yiyuan_id);
		$dataIntoSql['yiyuan_id'] = $yiyuan_id;
		$dataIntoSql['state'] = $state;
		$dataIntoSql['create_time'] = date("Y-m-d H:i:s");
		return self::add('zhuyuan_yizhu_zhixing_history_copy', $dataIntoSql);
		/*
		 $sql = "insert into zhuyuan_yizhu_zhixing_history_copy (".$re['val'].",state,yiyuan_id".") values (".$re['con'].",".$state.",'".$yiyuan_id."')";
		 return DB::query(Database::INSERT, $sql, false)->execute()['1'];
		 */
	}

	/**
	 * 获取所有需要排序数据
	 * @param string $zuhao
	 * @param string $yizhu_time
	 */
	public function getAllHistory($zuhao = '', $yizhu_time = '', $yiyuan_id){
		$sql_on = DB::select('id,real_time,chushi_state,yizhu_time,zhixing_state,op_type,dangqian_cishu,hedui_cishu,yizhu_yongfa_type as yongfa_type,type,zhixing_zuhao,zhuyuan_id')
		->from('zhuyuan_yizhu_zhixing_history_online')
		->where('zhixing_zuhao', '=', $zuhao)
		->and_where('is_op', '<', 2)
		->and_where('yiyuan_id', '=', $yiyuan_id);
		$sql_un = DB::select('id,real_time,chushi_state,yizhu_time,zhixing_state,op_type,dangqian_cishu,hedui_cishu,yizhu_yongfa_type as yongfa_type,type,zhixing_zuhao,zhuyuan_id')
		->from('zhuyuan_yizhu_zhixing_history_unline')
		->where('zhixing_zuhao', '=', $zuhao)
		->and_where('is_op', '<', 2)
		->and_where('yiyuan_id', '=', $yiyuan_id);
		if($yizhu_time){
			$sql_on->and_where('yizhu_time', '=', $yizhu_time);
			$sql_un->and_where('yizhu_time', '=', $yizhu_time);
		}
		$on_data = $sql_on->execute()->as_array();
		$un_data = $sql_un->execute()->as_array();

		return $on_data + $un_data;
		/*
		 $where = " where 1=1 ";
		 if($zuhao){
		 $where .= " and zuhao='".$zuhao."' ";
		 }
		 if($yizhu_time){
		 $where .= " and yizhu_time='".$yizhu_time."' ";
		 }
		 $sql = " select * from zhuyuan_yizhu_history_online ".$where." and is_op < 2 and yiyuan_id='".$yiyuan_id."'";
		 $on_data = DB::query(Database::SELECT, $sql)->execute()->as_array();

		 $sql = " select * from zhuyuan_yizhu_history_unline ".$where." and is_op < 2 and yiyuan_id='".$yiyuan_id."'";
		 $un_data = DB::query(Database::SELECT, $sql)->execute()->as_array();
		 return array_merge($on_data, $un_data);
		 */
	}

	/**
	 * 大排序数入库
	 * @param array $one
	 */
//	public function dataAddHistory($one = [], $yiyuan_id){
//        $values = "";
//        $keys = "";
//        foreach ($one as $key=>$item)
//        {
//            $item = sprintf("'%s'", $item);
//            if($values == "")
//            {
//                $values = $item;
//            }else{
//                $values .= ','.$item;
//            }
//            if($keys == '')
//            {
//                $keys = $key;
//            }else{
//                $keys .= ','.$key;
//            }
//        }
//		$count = DB::select('count(id) as tp_count')
//		->from('zhuyuan_yizhu_zhixing_history')
//		->where('id', '=', $one['id'])
//		->and_where('yiyuan_id', '=', $yiyuan_id)->execute()->as_array()['0']['tp_count'];
//		if(!$count){
//			$one['yiyuan_id'] = $yiyuan_id;
//            $sql = "insert into zhuyuan_yizhu_zhixing_history (".$keys.") select ".$values." from DUAL where not exists(select * from zhuyuan_yizhu_zhixing_history where zhuyuan_id='".$one['zhuyuan_id']."' and op_type='".$one['op_type']."' and zhixing_zuhao='".$one['zhixing_zuhao']."' and zhixing_state='".$one['zhixing_state']."' and dangqian_cishu='".$one['dangqian_cishu']."' and  case when zhixing_state in ('已校对','执行完毕') then 1=1 else hedui_cishu='".$one['hedui_cishu']."' end and case when type ='临时' then  1=1 else yizhu_time='".$one['yizhu_time']."' end)";
//            file_put_contents('zh1.log', print_r(array(date('Y-m-d H:i:s'), __LINE__, __METHOD__, $sql), TRUE));
//            return DB::query(Database::INSERT, $sql, false)->execute();
//            //return self::add('zhuyuan_yizhu_zhixing_history', $one);
//		}
//		return 1;
//
//
//       // $sql = "insert into zhuyuan_yizhu_zhixing_history (".implode(",",array_keys($one)).") select ".$values." from DUAL where not exists(select * from zhuyuan_yizhu_zhixing_history where zhuyuan_id='".$one['zhuyuan_id']."' and zhixing_zuhao='".$one['zhixing_zuhao']."' and zhixing_state='".$one['zhixing_state']."' and dangqian_cishu='".$one['dangqian_cishu']."' and  case when zhixing_state in ('已校对','执行完毕') then 1=1 else hedui_cishu='".$one['hedui_cishu']."' end and case when type ='临时' then  1=1 else yizhu_time='".$one['yizhu_time']."' end)";
//
//		// $sql = "insert into zhuyuan_yizhu_zhixing_history (".$dataIntoSql['val'].",yiyuan_id) select ".$dataIntoSql['con'].",".$yiyuan_id." from DUAL where not exists(select * from zhuyuan_yizhu_zhixing_history where id='".$one['history_id']."'";
//		 //return DB::query(Database::INSERT, $sql, false)->execute();
//
//	}

    public function dataAddHistory($one = [], $yiyuan_id){
        $hash_str = "";
        $hash_str = $one['zhuyuan_id'].$one['op_type'].$one['zhixing_zuhao'].$one['zhixing_state'].$one['dangqian_cishu'].time();
        if(!in_array($one['op_type'], ['核对','执行完毕'])){
            $hash_str.= $one['hedui_cishu'];
        }
        if($one['type'] == '长期'){
            $hash_str.= $one['yizhu_time'];
        }
        $hash_str = hash("md5",$hash_str);
        $one['yiyuan_id'] = $yiyuan_id;
        $one['business_time'] = $one['real_time'];

        $status = 0;
        try{
            $tag = self::add('zhuyuan_yizhu_zhixing_history_hash', array("hash_str"=>$hash_str,"addtime"=>time(),"yiyuan_id"=>$yiyuan_id));
            if($tag)
            {
                self::add('zhuyuan_yizhu_zhixing_history', $one);
                $status = 1;
            }
        }catch (Exception $exception) {
            $status = 0;
        }catch (Database_Expression $e){
            $status = 0;
        }

        return $status;
    }


	/**
	 * 删除数据
	 * @param array $ids
	 * @return number
	 */
	public function delHistory($ids = [], $yiyuan_id){
		return DB::delete('zhuyuan_yizhu_zhixing_history')
		->where('id', 'IN', $ids)
		->and_where('yiyuan_id', '=', $yiyuan_id)
		->execute();
		/*
		 $sql = "delete from zhuyuan_yizhu_zhixing_history where id in ('".implode("','", $ids)."') and yiyuan_id='".$yiyuan_id."'";
		 return DB::query(Database::DELETE, $sql, false)->execute()['1'];
		 */
	}
	/**
	 * 获取所有需要排序数据
	 * @param string $zuhao
	 * @param string $yizhu_time
	 */
	public function existDataToFlow($zuhao = '', $yizhu_time = '', $yiyuan_id){
		$sql_on = DB::select('*')
		->from('zhuyuan_yizhu_zhixing_history_online')
		->where('zhixing_zuhao', '=', $zuhao)
		->and_where('is_op', '=', 0)
		->and_where('yiyuan_id', '=', $yiyuan_id);
		$sql_un = DB::select('*')
		->from('zhuyuan_yizhu_zhixing_history_unline')
		->where('zhixing_zuhao', '=', $zuhao)
		->and_where('is_op', '=', 0)
		->and_where('yiyuan_id', '=', $yiyuan_id);
		if($yizhu_time){
			$sql_on->and_where('yizhu_time', '=', $yizhu_time);
			$sql_un->and_where('yizhu_time', '=', $yizhu_time);
		}
		$on_data = $sql_on->execute()->as_array();
		$un_data = $sql_un->execute()->as_array();

		return $on_data + $un_data;
		/*
		 $where = " where 1=1 ";
		 if($zuhao){
		 $where .= " and zuhao='".$zuhao."' ";
		 }
		 if($yizhu_time){
		 $where .= " and yizhu_time='".$yizhu_time."' ";
		 }
		 $sql = " select * from zhuyuan_yizhu_history_online ".$where." and is_op = 0 and yiyuan_id='".$yiyuan_id."'";
		 $on_data = DB::query(Database::SELECT, $sql)->execute()->as_array();

		 $sql = " select * from zhuyuan_yizhu_history_unline ".$where." and is_op = 0 and yiyuan_id='".$yiyuan_id."'";
		 $un_data = DB::query(Database::SELECT, $sql)->execute()->as_array();
		 return array_merge($on_data, $un_data);
		 */
	}

	/**
	 * 插入数据
	 * @param unknown $biao_name
	 * @param unknown $insert_data
	 * @return number
	 */
	public function add($biao_name, $insert_data)
	{
		$insert = DB::insert($biao_name)
		->columns(array_keys($insert_data))
		->values(array_values($insert_data));
		list($insert_id, $affected_rows) = $insert->execute();
		return $affected_rows;
	}

	/**
	 * 推送锁信息
	 * @param array $zuhao_arr
	 * @return void|boolean
	 */
	public function pushYizhuLockInfo($zuhao_arr = [], $set_data = []){
		$user_department_id = isset($set_data['user_department_id']) ? $set_data['user_department_id'] : $_REQUEST["user_department_id"];
		$user_number = isset($set_data['user_number']) ? $set_data['user_number'] : $_REQUEST["user_number"];
		$yiyuan_id = isset($set_data['yiyuan_id']) ? $set_data['yiyuan_id'] : $this->yiyuan_id;
		if(empty($zuhao_arr) || empty($user_department_id))
		{
			return;
		}
		$tmp = 0;
		$tmp_arr = array();
		foreach ($zuhao_arr as $zuhao => $one) {
			$tmp_arr[$tmp] = sprintf("update %s set yichang_lock = '%s' where zuhao = '%s'",
					($one['yizhu_time'] == date('Y-m-d') || empty($one['yizhu_time']))  ? 'yizhu_info' : 'yizhu_unwork_info',
					$one['yichang_lock'],
					$zuhao);
			$tmp++;
			if($one['yizhu_time']){
				$tmp_arr[$tmp] = sprintf("delete from yizhu_zhixing_history_lishi  where zhixing_zuhao = '%s' and yizhu_time='%s'",
						$zuhao,
						$one['yizhu_time']);
			}else{
				$tmp_arr[$tmp] = sprintf("delete from yizhu_zhixing_history_lishi  where zhixing_zuhao = '%s'",
						$zuhao);
			}
			$tmp++;
		}
		$ret_msg = json_encode(['update_sql' => $tmp_arr]);
		unset($tmp_arr);
		Utility::pushMsg($user_number, $user_department_id, $ret_msg, 0, 'shuaxin_yizhu', rand(), $yiyuan_id);
		return true;
	}
	/**
	 * 推送医嘱主表信息
	 * @param array $zuhao_arr 医嘱数据
	 */
	public function pushYizhuInfo($zuhao_arr = [], $set_data = []){
		$user_department_id = isset($set_data['user_department_id']) ? $set_data['user_department_id'] : $_REQUEST["user_department_id"];
		$user_number = isset($set_data['user_number']) ? $set_data['user_number'] : $_REQUEST["user_number"];
		$yiyuan_id = isset($set_data['yiyuan_id']) ? $set_data['yiyuan_id'] : $this->yiyuan_id;
		if(empty($zuhao_arr) || empty($user_department_id))
		{
			return;
		}
		$tmp = 0;
		$tmp_arr = array();
		foreach ($zuhao_arr as $zuhao => $one) {
			$tmp_arr[$tmp] = sprintf("update %s set zhixing_state = '%s', wancheng_cishu=%d, modify_time = '%s' where zuhao = '%s'",
					$one['yizhu_time'] == date('Y-m-d') ? 'yizhu_info' : 'yizhu_unwork_info',
					$one['zhixing_state'],
					$one['wancheng_cishu'],
					$one['modify_time'],
					$zuhao);
			$tmp++;
		}
		$ret_msg = json_encode(['update_sql' => $tmp_arr]);
		unset($tmp_arr);
		Utility::pushMsg($user_number, $user_department_id, $ret_msg, 0, 'shuaxin_yizhu', rand(), $yiyuan_id);
		return true;
	}

	/**
	 * 推送
	 * @param array $history_data 操作数据
	 */
	public function pushYizhuHistoryInfo($history_data = [], $set_data = []){
		$user_department_id = isset($set_data['user_department_id']) ? $set_data['user_department_id'] : $_REQUEST["user_department_id"];
		$user_number = isset($set_data['user_number']) ? $set_data['user_number'] : $_REQUEST["user_number"];
		$yiyuan_id = isset($set_data['yiyuan_id']) ? $set_data['yiyuan_id'] : $this->yiyuan_id;
		if(empty($history_data) || empty($user_department_id))
		{
			return;
		}
		if(empty($history_data)) return;
		$tiaoshu = 100;
		$number = ceil(count($history_data) / $tiaoshu);
		for ($i=0; $i < $number; $i++) {
			$value              = $i * $tiaoshu;
			$yizhu_data_content = array_slice($history_data, $value, $tiaoshu);

			$key = 0;
			foreach ($yizhu_data_content as $one_yizhu_info) {
				if ($key == 0) {
					$tmp_arr[$i] .= sprintf("REPLACE INTO yizhu_zhixing_history_lishi (history_id,zhuyuan_id,zhixing_state,zhixing_type,zhixing_hushi_id,zhixing_hushi_name,other_info,zhixing_zuhao,zhixing_time,type,beizhu,real_time,hedui_cishu,op_type,dangqian_cishu,yizhu_time,yizhu_shuxing,modify_time,yongfa_type,chushi_state) SELECT  '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'",
						 $one_yizhu_info["id"],
						 $one_yizhu_info["zhuyuan_id"],
						 $one_yizhu_info["zhixing_state"],
						 $one_yizhu_info["zhixing_type"],
						 $one_yizhu_info["zhixing_hushi_id"],
						 $one_yizhu_info["zhixing_hushi_name"],
						 $one_yizhu_info["other_info"],
						 $one_yizhu_info["zhixing_zuhao"],
						 $one_yizhu_info["zhixing_time"],
						 $one_yizhu_info["type"],
						 $one_yizhu_info["beizhu"],
						 $one_yizhu_info["real_time"],
						 $one_yizhu_info["hedui_cishu"],
						 $one_yizhu_info["op_type"],
						 $one_yizhu_info["dangqian_cishu"],
						 $one_yizhu_info["yizhu_time"],
						 $one_yizhu_info["yizhu_shuxing"],
						 $one_yizhu_info["create_time"],
						 $one_yizhu_info["yongfa_type"],
						 $one_yizhu_info["chushi_state"]
							);
				} else {
					$tmp_arr[$i] .= sprintf(" union all SELECT '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'",
						 $one_yizhu_info["id"],
						 $one_yizhu_info["zhuyuan_id"],
						 $one_yizhu_info["zhixing_state"],
						 $one_yizhu_info["zhixing_type"],
						 $one_yizhu_info["zhixing_hushi_id"],
						 $one_yizhu_info["zhixing_hushi_name"],
						 $one_yizhu_info["other_info"],
						 $one_yizhu_info["zhixing_zuhao"],
						 $one_yizhu_info["zhixing_time"],
						 $one_yizhu_info["type"],
						 $one_yizhu_info["beizhu"],
						 $one_yizhu_info["real_time"],
						 $one_yizhu_info["hedui_cishu"],
						 $one_yizhu_info["op_type"],
						 $one_yizhu_info["dangqian_cishu"],
						 $one_yizhu_info["yizhu_time"],
						 $one_yizhu_info["yizhu_shuxing"],
						 $one_yizhu_info["create_time"],
						 $one_yizhu_info["yongfa_type"],
						 $one_yizhu_info["chushi_state"]
							);
				}
				$key++;
			}
		}
		$ret_msg = json_encode(array('update_sql' => $tmp_arr));
		unset($tmp_arr);
		Utility::pushMsg($user_number, $user_department_id, $ret_msg, 0, 'shuaxin_yizhu', rand(), $yiyuan_id);
		return true;
	}
	
	/**
	 * 推送数据处理
	 * @param array $data
	 */
	public function pushMsgInsertQueese($data = []){
		$history_data = $data['history_data'];
		//操作数据
		if($history_data){
			$tiaoshu = 100;
			$number = ceil(count($history_data) / $tiaoshu);
			for ($i=0; $i < $number; $i++) {
				$value              = $i * $tiaoshu;
				$yizhu_data_content = array_slice($history_data, $value, $tiaoshu);
				$key = 0;
				foreach ($yizhu_data_content as $one_yizhu_info) {
					$tmp_arr[$i] .= sprintf(" %s SELECT  '%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s'",
							$key == 0 ? ' REPLACE INTO yizhu_zhixing_history_lishi (history_id,zhuyuan_id,zhixing_state,zhixing_type,zhixing_hushi_id,zhixing_hushi_name,other_info,zhixing_zuhao,zhixing_time,type,beizhu,real_time,hedui_cishu,op_type,dangqian_cishu,yizhu_time,yizhu_shuxing,modify_time,yongfa_type,chushi_state) ' : ' union all ',
							$one_yizhu_info["id"],
							$one_yizhu_info["zhuyuan_id"],
							$one_yizhu_info["zhixing_state"],
							$one_yizhu_info["zhixing_type"],
							$one_yizhu_info["zhixing_hushi_id"],
							$one_yizhu_info["zhixing_hushi_name"],
							$one_yizhu_info["other_info"],
							$one_yizhu_info["zhixing_zuhao"],
							$one_yizhu_info["zhixing_time"],
							$one_yizhu_info["type"],
							$one_yizhu_info["beizhu"],
							$one_yizhu_info["real_time"],
							$one_yizhu_info["hedui_cishu"],
							$one_yizhu_info["op_type"],
							$one_yizhu_info["dangqian_cishu"],
							$one_yizhu_info["yizhu_time"],
							$one_yizhu_info["yizhu_shuxing"],
							$one_yizhu_info["create_time"],
							$one_yizhu_info["yongfa_type"],
							$one_yizhu_info["chushi_state"]
							);
					$key++;
				}
			}
		}
		$zuhao_arr = $data['zuhao_arr'];
		//主表数据推送
		if($zuhao_arr){
			$tmp = count($tmp_arr);
			foreach ($zuhao_arr as $zuhao => $one) {
				if($one['yizhu_time']){
					if($one['yizhu_time'] >= date('Y-m-d')){
						$tmp_arr[$tmp] = sprintf("update %s set zhixing_state = '%s', wancheng_cishu=%d, modify_time = '%s' where zuhao = '%s' and yizhu_time='%s'",
								'yizhu_info' ,
								$one['zhixing_state'],
								$one['wancheng_cishu'],
								$one['modify_time'],
								$zuhao,
								$one['yizhu_time']);
					}else{
						$tmp_arr[$tmp] = sprintf("update %s set zhixing_state = '%s', wancheng_cishu=%d, modify_time = '%s' where zuhao = '%s' and unwork_time='%s'",
								'yizhu_unwork_info',
								$one['zhixing_state'],
								$one['wancheng_cishu'],
								$one['modify_time'],
								$zuhao,
								$one['yizhu_time']);
					}
					
				}else{//临时
					$tmp_arr[$tmp] = sprintf("update %s set zhixing_state = '%s', wancheng_cishu=%d, modify_time = '%s' where zuhao = '%s'",
							'yizhu_info',
							$one['zhixing_state'],
							$one['wancheng_cishu'],
							$one['modify_time'],
							$zuhao);
				}
				$tmp++;
			}
		}
		//锁定数据
		$lock_arr = $data['lock_arr'];
		if($lock_arr){
			$tmp = count($tmp_arr);
			foreach ($lock_arr as $zuhao => $one) {
				if($one['yizhu_time']){
					if($one['yizhu_time'] >= date('Y-m-d')){
						$tmp_arr[$tmp] = sprintf("update %s set yichang_lock = '%s' where zuhao = '%s' and yizhu_time='%s'",
								'yizhu_info',
								$one['yichang_lock'],
								$zuhao,
								$one['yizhu_time']);
					}else{
						$tmp_arr[$tmp] = sprintf("update %s set yichang_lock = '%s' where zuhao = '%s' and unwork_time='%s'",
								'yizhu_unwork_info',
								$one['yichang_lock'],
								$zuhao,
								$one['yizhu_time']);
					}
					
					$tmp++;
					$tmp_arr[$tmp] = sprintf("delete from yizhu_zhixing_history_lishi  where zhixing_zuhao = '%s' and yizhu_time='%s'",
							$zuhao,
							$one['yizhu_time']);
				}else{//临时
					$tmp_arr[$tmp] = sprintf("update %s set yichang_lock = '%s' where zuhao = '%s'",
							'yizhu_info',
							$one['yichang_lock'],
							$zuhao);
					$tmp++;
					$tmp_arr[$tmp] = sprintf("delete from yizhu_zhixing_history_lishi  where zhixing_zuhao = '%s'",
							$zuhao);
				}
				$tmp++;
			}
		}
		//添加队列表
		if($tmp_arr){
			$user_department_id = isset($set_data['user_department_id']) ? $set_data['user_department_id'] : $_REQUEST["user_department_id"];
			$shebei_id = isset($set_data['shebei_id']) ? $set_data['shebei_id'] : $_REQUEST["shebei_id"];
			$yiyuan_id = isset($set_data['yiyuan_id']) ? $set_data['yiyuan_id'] : $this->yiyuan_id;
			$user_number  = isset($set_data['user_number']) ? $set_data['user_number'] : $_REQUEST["user_number"];			
			$ret_msg = json_encode(array('update_sql' => $tmp_arr));
			$insert_data = array();
			$insert_data['content'] = base64_encode($ret_msg);
			$insert_data['user_department_code'] = $user_department_id;
			$insert_data['user_department'] = isset($set_data['user_department']) ? $set_data['user_department'] : $_REQUEST["user_department"];
			$insert_data['msg_type'] = "shuaxin_yizhu";
			$insert_data['expiry_time'] = time();
			$insert_data['create_at'] = time();
			$insert_data['yiyuan_id'] = $yiyuan_id;
			$insert_data['shebei_id'] = $shebei_id;
			self::add("pushmsg_queue",$insert_data);
			unset($tmp_arr);
			//Utility::pushMsg($user_number, $user_department_id, $ret_msg, 0, 'shuaxin_yizhu', rand(), $yiyuan_id);
			return true;
		}
		return false;
	}
}
